package com.shejimoshi.单例模式.深浅克隆.深克隆.实现cloneAble进行深度复制;

import java.util.Arrays;

public class Person implements Cloneable {
    private String name;
    private int age;
    private Food food;
    private Integer[] somtins;

    public Person(String name, int age, Food food) {
        this.name = name;
        this.age = age;
        this.food = food;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    public Integer[] getSomtins() {
        return somtins;
    }

    public void setSomtins(Integer[] somtins) {
        this.somtins = somtins;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person person = null;
        try{
            person = (Person) super.clone();
            person.somtins = Arrays.copyOf(this.somtins, this.somtins.length); // 对数组的深度复制
            // person.list = person.list.addAll(this.list);  // 如果是list就采用addAll进行深度复制, remove(index/object)
            // person.set = person.set.addAll(this.set);  // 如果是list就采用addAll进行深度复制, remove(object)
            // person.map = person.map.putAll(this.map);  // 如果是list就采用putAll进行深度复制, remove(key)

            person.food = (Food) this.food.clone();
        } catch (Exception e) {
            System.out.println("person拷贝对象出错了！");
        }
        return person;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", food=" + food +
                ", somtins=" + Arrays.toString(somtins) +
                '}';
    }
}
